$(document).ready(function() {

    var socket = new SockJS('/bpo-raffle-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        // setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/raffle/zhongjiang', function (bingo) {
            showGreeting(JSON.parse(bingo.body).content);

        });
    });
});

var users = [];
function showGreeting(message) {
    console.log(message);
    users.push(message);
    var vHtml = "";
    for(var i=1;i<(users.length+1);i++){
        let cname = users[i-1];
        vHtml += i+" : "+cname+"&nbsp;&nbsp;&nbsp;&nbsp;";
        if (i % 4 == 0){
            vHtml += "<br>";
        }
    }
    document.getElementById('userName').innerHTML = vHtml;

}

var stompClient = null;
function disconnect() {
    if (stompClient !== null) {
        stompClient.disconnect();
    }
    // setConnected(false);
    console.log("Disconnected");
}


///////////////////////////////////
/**可选号码总数，即需要操作次数*/
var _MAX_NUM = 100;
/**更新号码的时间间隔，越小越快*/
var _SPEED = 60;
///////////////////////////////////

/**存储未被选中的号码*/
var m_oriNumArr = new Array();
for(var i=1;i<=_MAX_NUM;i++){
    m_oriNumArr[i-1] = i;
}
/**存储已经产生的号码*/
var m_selNumArr = new Array();
/**每次开始选号的定时器编号*/
var m_timmerId = 0;
var btnBool = 0;
function reStart(){
    var mymessage=confirm("确认要重新开始游戏吗？");
    if(mymessage==true)
    {
        stompClient.send("/bpo/restart", {}, JSON.stringify({'name':'1'}));
        window.location.reload();
    }
}
/**响应开始按钮*/
function startShow(){
    if (nation_data.length == 0)
    {
      return;
    }
    if (btnBool == 0) {

        //当前操作序号加1，开始按钮不可再点击，把光标(焦点)移到停止按钮
        document.getElementById("seq").innerText = parseInt(document.getElementById("seq").innerText)+1;
        $("#btnStart").val('停 止');
        $("#btnStart").css("background-color","turquoise");
        m_timmerId = window.setInterval(genRandomNum, _SPEED);
        btnBool = 1;
    }  else  {
        endShow();
        btnBool = 0;
    }
        // parseInt(document.getElementById("seq").innerText)+1;
    // document.getElementById("btnStart").disabled = true;
    // document.getElementById('btnEnd').focus();
    //启动定时获取随机数


}

/**随机产生一个数组的索引，通过索引取得数组中的值并显示到页面的DIV中*/
function genRandomNum(){
    var temp = parseInt(Math.random() * nation_data.length);
    // console.log(m_oriNumArr);
    // console.log(temp);
    // var tt =  parseInt(m_oriNumArr[temp]);
    document.getElementById("divNum").innerText = nation_data[temp]['name'];
}

var names = [];
/**响应停止按钮*/
function endShow(){
    if(m_timmerId == 0)return;//未点击开始按钮时不响应
    $("#btnStart").val('开 始');
    // $("#btnStart").attr('onclick','').click( eval(function(){startShow()}));
    $("#btnStart").css("background-color","coral");
    //取消定时获取随机数
    window.clearInterval(m_timmerId);
    m_timmerId = 0;

    //取得页面显示DIV中的值，存入已选数组
    var selname = document.getElementById("divNum").innerText;
    var selValue = 0;
    for (x in nation_data){
        let code_name = nation_data[x]["name"];

        if (code_name==selname) {
            names.push(code_name);
            selValue = x;
            let codeid = nation_data[x]["zip"]
            // console.log(selValue);
            let dicstr = JSON.stringify({"name":code_name,"zip":codeid});
            stompClient.send("/bpo/yaohao", {}, JSON.stringify({'name': dicstr}));
            nation_data.splice(x,1);
            break;
        }
    }
    console.log(nation_data);
    m_selNumArr.push(selValue);


    //从原始数据中删除被选中的号码
    remove(selValue);


    //所有号码选完后开始按钮不再可用
    // if(m_selNumArr.length != _MAX_NUM){
    // document.getElementById("btnStart").disabled = false;
    // document.getElementById('btnStart').focus();
    // }else{//显示当前序号对应的号码
    var vHtml = "";
    for(var i=1;i<(names.length+1);i++){
        let cname = names[i-1];
        vHtml += i+" : "+cname+"&nbsp;&nbsp;&nbsp;&nbsp;";
        if (i % 4 == 0){
            vHtml += "<br>";
        }
    }
    document.getElementById('divData').innerHTML = vHtml;
    // }
}
/**删除被选中的号码*/
function remove(vData){
    if(m_oriNumArr.length == 0)return;//数组已经没有数据
    //查找到选中号码的索引
    var index = -1;
    for(var i=0;i<m_oriNumArr.length;i++){
        if(vData == m_oriNumArr[i]){
            index = i;
        }
    }
    if(-1 == index)return;//未找到不处理

    //将最后一个号码调到选中号码所在位置，然后删除最后一个
    m_oriNumArr[index] = m_oriNumArr[m_oriNumArr.length-1];
    m_oriNumArr.length -= 1;
}
var nation_data = [
    {label:"北京Beijing010",name:"北京",pinyin:"Beijing",zip:"010"},
    {label:"重庆Chongqing023",name:"重庆",pinyin:"Chongqing",zip:"023"},
    {label:"上海Shanghai021",name:"上海",pinyin:"Shanghai",zip:"021"},
    {label:"天津Tianjin022",name:"天津",pinyin:"Tianjin",zip:"022"},
    {label:"长春Changchun0431",name:"长春",pinyin:"Changchun",zip:"0431"},
    {label:"长沙Changsha0731",name:"长沙",pinyin:"Changsha",zip:"0731"},
    {label:"常州Changzhou0519",name:"常州",pinyin:"Changzhou",zip:"0519"},
    {label:"成都Chengdu028",name:"成都",pinyin:"Chengdu",zip:"028"},
    {label:"大连Dalian0411",name:"大连",pinyin:"Dalian",zip:"0411"},
    {label:"东莞Dongguan0769",name:"东莞",pinyin:"Dongguan",zip:"0769"},
    {label:"佛山Foshan0757",name:"佛山",pinyin:"Foshan",zip:"0757"},
    {label:"福州Fuzhou0591",name:"福州",pinyin:"Fuzhou",zip:"0591"},
    {label:"广州Guangzhou020",name:"广州",pinyin:"Guangzhou",zip:"020"},
    {label:"贵阳Guiyang0851",name:"贵阳",pinyin:"Guiyang",zip:"0851"},
    {label:"哈尔滨Haerbin0451",name:"哈尔滨",pinyin:"Haerbin",zip:"0451"},
    {label:"海口Haikou0898",name:"海口",pinyin:"Haikou",zip:"0898"},
    {label:"邯郸Handan0310",name:"邯郸",pinyin:"Handan",zip:"0310"},
    {label:"襄樊Xiangfan0710",name:"襄樊",pinyin:"Xiangfan",zip:"0710"},
    {label:"合肥Hefei0551",name:"合肥",pinyin:"Hefei",zip:"0551"},
    {label:"惠州Huizhou0752",name:"惠州",pinyin:"Huizhou",zip:"0752"},
    {label:"焦作Jiaozuo0391",name:"焦作",pinyin:"Jiaozuo",zip:"0391"},
    {label:"嘉兴Jiaxing0573",name:"嘉兴",pinyin:"Jiaxing",zip:"0573"},
    {label:"吉林Jilin0423",name:"吉林",pinyin:"Jilin",zip:"0423"},
    {label:"济南Jinan0531",name:"济南",pinyin:"Jinan",zip:"0531"},
    {label:"昆明Kunming0871",name:"昆明",pinyin:"Kunming",zip:"0871"},
    {label:"兰州Lanzhou0931",name:"兰州",pinyin:"Lanzhou",zip:"0931"},
    {label:"柳州Liuzhou0772",name:"柳州",pinyin:"Liuzhou",zip:"0772"},
    {label:"洛阳Luoyang0379",name:"洛阳",pinyin:"Luoyang",zip:"0379"},
    {label:"南昌Nanchang0791",name:"南昌",pinyin:"Nanchang",zip:"0791"},
    {label:"南京Nanjing025",name:"南京",pinyin:"Nanjing",zip:"025"},
    {label:"南宁Nanning0771",name:"南宁",pinyin:"Nanning",zip:"0771"},
    {label:"南通Nantong0513",name:"南通",pinyin:"Nantong",zip:"0513"},
    {label:"宁波Ningbo0574",name:"宁波",pinyin:"Ningbo",zip:"0574"},
    {label:"青岛Qingdao0532",name:"青岛",pinyin:"Qingdao",zip:"0532"},
    {label:"泉州Quanzhou0595",name:"泉州",pinyin:"Quanzhou",zip:"0595"},
    {label:"沈阳Shenyang024",name:"沈阳",pinyin:"Shenyang",zip:"024"},
    {label:"深圳Shenzhen0755",name:"深圳",pinyin:"Shenzhen",zip:"0755"},
    {label:"石家庄Shijiazhuang0311",name:"石家庄",pinyin:"Shijiazhuang",zip:"0311"},
    {label:"苏州Suzhou0512",name:"苏州",pinyin:"Suzhou",zip:"0512"},
    {label:"台州Taizhou0576",name:"台州",pinyin:"Taizhou",zip:"0576"},
    {label:"唐山Tangshan0315",name:"唐山",pinyin:"Tangshan",zip:"0315"},
    {label:"潍坊Weifang0536",name:"潍坊",pinyin:"Weifang",zip:"0536"},
    {label:"威海Weihai0631",name:"威海",pinyin:"Weihai",zip:"0631"},
    {label:"武汉Wuhan027",name:"武汉",pinyin:"Wuhan",zip:"027"},
    {label:"无锡Wuxi0510",name:"无锡",pinyin:"Wuxi",zip:"0510"},
    {label:"厦门Xiamen0592",name:"厦门",pinyin:"Xiamen",zip:"0592"},
    {label:"西安Xian029",name:"西安",pinyin:"Xian",zip:"029"},
    {label:"许昌Xuchang0374",name:"许昌",pinyin:"Xuchang",zip:"0374"},
    {label:"徐州Xuzhou0516",name:"徐州",pinyin:"Xuzhou",zip:"0516"},
    {label:"扬州Yangzhou0514",name:"扬州",pinyin:"Yangzhou",zip:"0514"},
    {label:"烟台Yantai0535",name:"烟台",pinyin:"Yantai",zip:"0535"},
    {label:"漳州Zhangzhou0596",name:"漳州",pinyin:"Zhangzhou",zip:"0596"},
    {label:"郑州Zhengzhou0371",name:"郑州",pinyin:"Zhengzhou",zip:"0371"},
    {label:"中山Zhongshan0760",name:"中山",pinyin:"Zhongshan",zip:"0760"},
    {label:"珠海Zhuhai0756",name:"珠海",pinyin:"Zhuhai",zip:"0756"},
    {label:"秦皇岛Qinhuangdao0335",name:"秦皇岛",pinyin:"Qinhuangdao",zip:"0335"},
    {label:"临沂Linyi0539",name:"临沂",pinyin:"Linyi",zip:"0539"},
    {label:"安康Ankang0915",name:"安康",pinyin:"Ankang",zip:"0915"},
    {label:"安庆Anqing0556",name:"安庆",pinyin:"Anqing",zip:"0556"},
    {label:"鞍山Anshan0412",name:"鞍山",pinyin:"Anshan",zip:"0412"},
    {label:"安阳Anyang0372",name:"安阳",pinyin:"Anyang",zip:"0372"},
    {label:"白城Baicheng0436",name:"白城",pinyin:"Baicheng",zip:"0436"},
    {label:"百色Baise0776",name:"百色",pinyin:"Baise",zip:"0776"},
    {label:"白山Baishan0439",name:"白山",pinyin:"Baishan",zip:"0439"},
    {label:"白银Baiyin0943",name:"白银",pinyin:"Baiyin",zip:"0943"},
    {label:"蚌埠Bangbu0552",name:"蚌埠",pinyin:"Bangbu",zip:"0552"},
    {label:"保定Baoding0312",name:"保定",pinyin:"Baoding",zip:"0312"},
    {label:"宝鸡Baoji0917",name:"宝鸡",pinyin:"Baoji",zip:"0917"},
    {label:"保山Baoshan0875",name:"保山",pinyin:"Baoshan",zip:"0875"},
    {label:"包头Baotou0472",name:"包头",pinyin:"Baotou",zip:"0472"},
    {label:"巴中Bazhong0827",name:"巴中",pinyin:"Bazhong",zip:"0827"},
    {label:"北海Beihai0779",name:"北海",pinyin:"Beihai",zip:"0779"},
    {label:"本溪Benxi0414",name:"本溪",pinyin:"Benxi",zip:"0414"},
    {label:"滨州Binzhou0543",name:"滨州",pinyin:"Binzhou",zip:"0543"},
    {label:"亳州Bozhou0558",name:"亳州",pinyin:"Bozhou",zip:"0558"},
    {label:"沧州Cangzhou0317",name:"沧州",pinyin:"Cangzhou",zip:"0317"},
    {label:"常德Changde0736",name:"常德",pinyin:"Changde",zip:"0736"},
    {label:"昌都Changdu0895",name:"昌都",pinyin:"Changdu",zip:"0895"},
    {label:"昌吉Changji0997",name:"昌吉",pinyin:"Changji",zip:"0997"},
    {label:"长治Changzhi0355",name:"长治",pinyin:"Changzhi",zip:"0355"},
    {label:"巢湖Chaohu0565",name:"巢湖",pinyin:"Chaohu",zip:"0565"},
    {label:"朝阳Chaoyang0421",name:"朝阳",pinyin:"Chaoyang",zip:"0421"},
    {label:"潮州Chaozhou0768",name:"潮州",pinyin:"Chaozhou",zip:"0768"},
    {label:"赤峰Chifeng0476",name:"赤峰",pinyin:"Chifeng",zip:"0476"},
    {label:"池州Chizhou0566",name:"池州",pinyin:"Chizhou",zip:"0566"},
    {label:"金华Jinhua0579",name:"金华",pinyin:"Jinhua",zip:"0579"},
    {label:"楚雄Chuxiong0879",name:"楚雄",pinyin:"Chuxiong",zip:"0879"},
    {label:"滁州Chuzhou0550",name:"滁州",pinyin:"Chuzhou",zip:"0550"},
    {label:"大理Dali0872",name:"大理",pinyin:"Dali",zip:"0872"},
    {label:"丹东Dandong0415",name:"丹东",pinyin:"Dandong",zip:"0415"}
];
